﻿using System;
using System.Collections.Generic;
using System.Text;

namespace testca
{
    class pmem_x86_64 : IPhysMemAllocator
    {
        const ulong DMA_START = 0x100000;
        const ulong DMA_END = 0x1000000;
        const ulong MAIN_START = 0x1000000;
        const ulong MAIN_END = 0x100000000;

        const ulong PAGE_SIZE = 0x1000;

        #region IPhysMemAllocator Members

        public void Initialise(vmem vmem)
        {
            throw new NotImplementedException();
        }

        public ulong BeginAlloc()
        {
            throw new NotImplementedException();
        }

        public ulong EndAlloc(ulong vaddr, ulong paddr)
        {
            throw new NotImplementedException();
        }

        public void Free(ulong vaddr, ulong paddr)
        {
            if (paddr < DMA_END)
            {
                if (paddr >= DMA_START)
                    bitmap.Free(paddr);
            }
            else
            {
                if (paddr < MAIN_END)
                    stack.Push(vaddr, paddr);
            }
        }

        public ulong AllocRange(ulong length)
        {
            throw new NotImplementedException();
        }

        #endregion

        PhysMemBitmap bitmap;
        PhysMemStack stack;

        public pmem_x86_64()
        {
            bitmap = new PhysMemBitmap(DMA_START, DMA_END, PAGE_SIZE);
            stack = new PhysMemStack();
        }

        #region IPhysMemAllocator Members


        public ulong GetFreePageCount()
        {
            return bitmap.Count + stack.Count;
        }

        public ulong GetFreeMemory()
        {
            return GetFreePageCount() * PAGE_SIZE;
        }

        #endregion
    }
}
